<?php

namespace app\master\controller;

use think\Db;
use think\Exception;

class Member extends Base
{
    //会员列表
    public function member()
    {
        if (request()->isGet()) {
            return $this->fetch('member');
        }
        $page = input("page");
        $limit = input("limit");
        $list = model('member')->member($page, $limit);
        return $this->tableReturn(0, '查询成功', $list['count'], $list['data']);
    }

    //添加会员
    public function addmember()
    {
        if (request()->isGet()) {
            $list = model('company')->getwherelist(['pid' => 0]);
            $this->assign('company', $list);
            return $this->fetch("addmember");
        }
        $data = input('post.');
        $department_id = db('company')->where('company_id', $data['department_id'])->value('department_id');
        $member = [
            "member_name" => $data['member_name'],
            "member_tel" => $data['member_tel'],
            "member_card" => $data['member_card'],
            "member_sex" => $data['member_sex'],
            "company_id" => $data['company_id'],
            "member_type" => 1,
            "department_id" => $department_id,
            "member_isvip" => 2,
            "member_ifcar" => 1,
            "member_status" => 1,
        ];
        $list = db('member')->insert($member);
        if ($list) {
            $this->ajaxReturn(200, successMsg, url("Member/member"));
        } else {
            $this->ajaxReturn(603, errorMsg);
        }
    }

    //设置vip
    public function setupvip()
    {
        $member_id = input('member_id');
        $data = db('member')->where('member_id', $member_id)->find();
        if ($data['member_isvip'] == 1) $this->ajaxReturn(603, 此用户已经是vip);
        $list = db('member')->where('member_id', $member_id)->update(['member_isvip' => 1]);
        if ($list) {
            $this->ajaxReturn(200, successMsg);
        } else {
            $this->ajaxReturn(603, errorMsg);
        }
    }

    //查看会员约车记录
    public function lookaboutcar()
    {
        $id = input('member_id');
        if (request()->isGet()) {
            $this->assign('member_id', $id);
            return $this->fetch("lookaboutcar");
        }
        $where = [
            'co.member_id' => $id,
          //  'co.contractcar_types' => 3,
            'co.contractcar_status' => 4,
        ];
        $page = input('page');
        $limit = input('limit');
        $field = "from_unixtime(co.contractcar_bigontime,'%Y .%m .%d %H:%i:%s') as contractcar_bigontime,from_unixtime(co.contractcar_endtime,'%Y .%m .%d %H:%i:%s') as contractcar_endtime,co.contractcar_type,c.car_name,c.car_plate";
        $list = model('Contractcar')->getContractCarLog($field, $where, $page, $limit);
        return $this->tableReturn(0, '查询成功', $list['count'], $list['data']);
    }
}